// lib/models/product.dart
import 'package:json_annotation/json_annotation.dart';
import 'sku.dart';
part 'product.g.dart';

@JsonSerializable(explicitToJson: true) // 嵌套对象 toJson 需要这一行
class Product {
  final int id;
  final int categoryId;
  final int defaultSkuId;
  final String? description;
  @JsonKey(name: 'imageUrl') // 字段重命名
  final String imageUrl;
  final int merchantId;
  final String name;
  final List<Sku> skus;
  final int status;
  final int sales;
  Product({
    required this.id,
    required this.name,
    required this.imageUrl,
    this.description,
    required this.defaultSkuId,
    required this.categoryId,
    required this.merchantId,
    required this.status,
    required this.sales,
    List<Sku>? skus, // 接受可空
  }) : skus = skus ?? const []; // 用构造器兜底

  Sku? get defaultSku => skus.where((e) => e.id == defaultSkuId).firstOrNull;

  factory Product.fromJson(Map<String, dynamic> json) =>
      _$ProductFromJson(json);
  Map<String, dynamic> toJson() => _$ProductToJson(this);
}
